package am.aua.renju.web.filldata;

import am.aua.renju.databaseaccess.GameOperations;
import am.aua.renju.entity.Game;
import am.aua.renju.entity.Player;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Index {

    private static class AjaxDataTopPlayer {

        public AjaxDataTopPlayer(String nickname, int rating, int rank) {
            this.nickname = nickname;
            this.rating = rating;
            this.rank = rank;
        }
        public String nickname;
        public int rating;
        public int rank;
    }

    public static String loadAjaxTop10Players(List<Player> top10Players) {
        AjaxDataTopPlayer[] ajaxDataTopPlayers = new AjaxDataTopPlayer[10];
        for (int i = 0; i < top10Players.size(); i++) {
            ajaxDataTopPlayers[i]=new AjaxDataTopPlayer(top10Players.get(i).getNickname(), top10Players.get(i).getRating(), i + 1);            
        }
        Gson gson=new Gson();
        return gson.toJson(ajaxDataTopPlayers, AjaxDataTopPlayer[].class);
    }

    private static class AjaxData {

        public AjaxData(String playerName, int rating, int time, boolean startRule, boolean isOwner) {
            this.playerName = playerName;
            this.rating = rating;
            this.startRule = startRule;
            this.time = time;
            this.isOwner = isOwner;
        }
        public String playerName;
        public int rating;
        public int time;
        public boolean startRule;
        public boolean isOwner;
    }

    public static Data loadErrorGameDuplication(Map root) {
        root.put("createGameError", "");
        root.put("createGameDuplicationError", "There is already a created game with same parameters.");
        return new Data("index.html", root);
    }

    public static Data loadErrorCreateGameLimit(Map root) {
        root.put("createGameDuplicationError", "");
        root.put("createGameError", "It is not allowed to create more than 5 games.");
        return new Data("index.html", root);
    }

    public static Data load(Map root) {
        root.put("createGameError", "");
        root.put("createGameDuplicationError", "");
        root.put("createGame", "Create Game");
        root.put("gameListError", "");
        return new Data("index.html", root);
    }

    public static String loadAjaxCreateGame() {
        return "";
    }

    public static String loadAjaxGameData(String email) throws IOException {
        List<Game> games = GameOperations.getInstance().getWaitGames();
        List<AjaxData> ajaxDatas = new LinkedList<AjaxData>();
        for (Game g : games) {
            ajaxDatas.add(new AjaxData(g.getPlayer1Id().getNickname(), g.getPlayer1Id().getRating(), g.getTime() == null ? 0 : g.getTime(), g.getStartRules() != 0, email.equals(g.getPlayer1Id().getEmail())));
        }
        Gson gson = new Gson();
        return gson.toJson(ajaxDatas, List.class);
    }
}
